const SortingAlgorithms = require("../template")

function testSortingComparison() {
  const sorter = new SortingAlgorithms()

  console.log(
    "1. 算法结果一致性测试:",
    (() => {
      const testArray = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
      const expected = [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...testArray])) === JSON.stringify(expected))
    })()
  )

  console.log(
    "2. 负数排序测试:",
    (() => {
      const negativeArray = [-5, -1, -3, -2, -4]
      const expectedNegative = [-5, -4, -3, -2, -1]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...negativeArray])) === JSON.stringify(expectedNegative))
    })()
  )

  console.log(
    "3. 零值排序测试:",
    (() => {
      const zeroArray = [0, 5, 0, 3, 0, 1]
      const expectedZero = [0, 0, 0, 1, 3, 5]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...zeroArray])) === JSON.stringify(expectedZero))
    })()
  )

  console.log(
    "4. 混合正负数排序测试:",
    (() => {
      const mixedArray = [-3, 1, -4, 2, -5, 0]
      const expectedMixed = [-5, -4, -3, 0, 1, 2]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...mixedArray])) === JSON.stringify(expectedMixed))
    })()
  )

  console.log(
    "5. 大数排序测试:",
    (() => {
      const largeArray = [1000, 1, 999, 2, 998, 3]
      const expectedLarge = [1, 2, 3, 998, 999, 1000]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...largeArray])) === JSON.stringify(expectedLarge))
    })()
  )

  console.log(
    "6. 小数排序测试:",
    (() => {
      const decimalArray = [3.1, 1.5, 2.9, 1.1, 3.0]
      const expectedDecimal = [1.1, 1.5, 2.9, 3.0, 3.1]
      const algorithms = ["bubbleSort", "selectionSort", "insertionSort", "mergeSort", "quickSort", "heapSort"]
      return algorithms.every(algo => JSON.stringify(sorter[algo]([...decimalArray])) === JSON.stringify(expectedDecimal))
    })()
  )

  console.log(
    "7. this上下文排序比较测试:",
    (() => {
      const sortObj = {
        multiplier: 2,
        processComparison: function(arr) {
          const sorter = new SortingAlgorithms()
          const result = sorter.bubbleSort([...arr])
          return result.map(item => item * this.multiplier)
        }
      }
      const result = sortObj.processComparison([3, 1, 2])
      return JSON.stringify(result) === JSON.stringify([2, 4, 6])
    })()
  )

  console.log(
    "8. 复杂对象this绑定测试:",
    (() => {
      const sortConfigObj = {
        config: { offset: 5 },
        processWithConfig: function(arr) {
          const sorter = new SortingAlgorithms()
          const result = sorter.selectionSort([...arr])
          return result.map(item => item + this.config.offset)
        }
      }
      const result = sortConfigObj.processWithConfig([2, 1, 3])
      return JSON.stringify(result) === JSON.stringify([6, 7, 8])
    })()
  )
}

testSortingComparison()
